<?php

$CONTENT_FOLDER = 'c:/Daniel/php/pictures/content/';
	
$MACHINE = 'localhost';
$LOGIN  = 'pictures';
$PASSWORD = 'please enter password here';
$DATABASE = 'pictures';
$PORT	 = 3306;
    
$JOB_STATUS_BRAND_NEW = 'brand new';
$JOB_STATUS_READY_PROCESS = 'ready to process';
$JOB_STATUS_PENDING = 'pending';
$JOB_STATUS_PROCESSED = 'processed';

$ZIP_COMMAND = '"C:\Program Files\7-Zip\7z" a';

class Job {
	private $id; 
	private $name; 
	private $userId; 
	private $status; 
	
	public function Job($i, $nm, $user, $stat){
		$this->id = $i;
		$this->name = $nm;
		$this->userId = $user;
		$this->status = $stat;
	}
	
	public function getId(){return $this->id; }
	public function getName(){return $this->name; }
	public function getUserId(){return $this->userId; }
	public function getStatus(){return $this->status; }
}


function getUpdateIds($conn){
	global $JOB_STATUS_READY_PROCESS;
	$ids = array();
	$sql = 'select job_id from job where status_cd = ?'; 
	
	$stat = $conn->prepare($sql);
	$stat->bind_param('s', $JOB_STATUS_READY_PROCESS); 
	$stat->bind_result($id);
	
	$stat->execute();
	while($stat->fetch()){
		$ids[] = $id;
	}
	
	return $ids;
}

function getJob($conn, $id){
	$sql = 'select job_id, user_id, job_name, status_cd from job where job_id = ?';
	$stat = $conn->prepare($sql);
	$stat->bind_param('s', $id);
	$stat->bind_result($jid, $user, $name, $status);
	
	$stat->execute();
	
	$res = null;
	
	if($stat->fetch()){
		$res = new Job($jid, $name, $user, $status);
	}
	
	return $res; 
}

function setPending($conn, $id){
	global $JOB_STATUS_PENDING;
	setStatus($conn, $id, $JOB_STATUS_PENDING);
}

function getArchiveName($id){
	global $CONTENT_FOLDER;
	
	return "{$CONTENT_FOLDER}jobs/job_{$id}.dat";
}

function getPictureIds($conn, $id){
	$res = array();
	
	$sql = 'select picture_id from job_picture where job_id = ?';
	$stat = $conn->prepare($sql);
	
	$stat->bind_param('s', $id);
	$stat->bind_result($resid);
	$stat->execute();
	
	while($stat->fetch()){
		$res[] = $resid;
	}
	
	return $res;
}

function processArchive($conn, $id){
	global $CONTENT_FOLDER, $ZIP_COMMAND;
	
	
	$name = getArchiveName($id);
	
	if(is_file($name)){
		unlink($name);
	}

	// get the ids of the pictures
	$ids = getPictureIds($conn, $id);
	$files = array();
	
	foreach($ids as $current){
		$files[] = "{$CONTENT_FOLDER}pictures/full_{$current}.dat";
	}
	
	$joined = join(" ", $files);
	$command = "{$ZIP_COMMAND} {$name} {$joined}";
	
	exec($command);
}

function setProcessed($conn, $id){
	global $JOB_STATUS_PROCESSED;
	
	setStatus($conn, $id, $JOB_STATUS_PROCESSED);
}

function setStatus($conn, $id, $status){
	$sql = 'update job set status_cd = ? where job_id = ?';
	$stat = $conn->prepare($sql);
	
	$stat->bind_param('ss', $status, $id);
	$stat->execute();
}

function getConnection(){
	global $MACHINE, $LOGIN, $PASSWORD, $DATABASE, $PORT;
	return new mysqli($MACHINE, $LOGIN, $PASSWORD, $DATABASE, $PORT);
}

try {
	$conn = getConnection();
	$ids = getUpdateIds($conn);
	
	foreach($ids as $id){
		$job = getJob($conn, $id);
		
		if($job != null){
			if($JOB_STATUS_READY_PROCESS == $job->getStatus()){
				setPending($conn, $id);
				
				processArchive($conn, $id); 
				
				setProcessed($conn, $id);
			}
		}
	} 
	
	$conn->close();
}
catch(Exception $e){
	echo $e->getMessage();
}

?>